
package com.hmsg.health.dao;

import com.hmsg.health.bean.Dto.TaskDto;
import com.hmsg.health.bean.domain.TaskDo;
import com.hmsg.health.bean.domain.UserDo;

import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface TaskDao {
	
    @Select("select * from s_task where id=#{id}")
    @Results({
        @Result(column="user_id",property="user",one=@One(select="com.hmsg.health.dao.UserDao.select")),
        @Result(column="type",property="typeDo",one=@One(select="com.hmsg.health.dao.TaskTypeDao.findOne"))
    })
    TaskDo findOne(@Param("id")Integer id);
    
   
    
    
    @Select("<script>" +
    	    "select * from  `s_task` t left join `s_task_type` tt on t.type=tt.id where 1=1 " +
    	    "    <if test='info.id != null'>" +
    	    "     and t.id = #{info.id} " +
    	    "    </if>" +
    	
    	    "    <if test='info.type != null and info.type != -1'>" +
    	    "    and  tt.parent_id = #{info.type} " +
    	    "    </if>" +
    	    "    <if test='info.type ==-1'>" +
    	    "    and  t.type = -1 " +
    	    "    </if>" +
    	    "    <if test='info.taskVerify != null'>" +
    	    "    and  t.task_verify = #{info.taskVerify} " +
    	    "    </if>" +
    	    "    <if test='info.taskStatus != null'>" +
    	    "      and t.task_status  = #{info.taskStatus} " +
    	    "    </if>" +
    	    "    <if test='info.isRecommend != null'>" +
    	    "      and t.is_recommend = #{info.isRecommend}  " +
    	    "    </if>" +
    	    "    <if test='info.userId != null'>" +
    	    "      and t.user_id = #{info.userId}  " +
    	    "    </if>" +
    	    "    <if test='info.isHot != null'>" +
    	    "      and t.is_hot = #{info.isHot}  " +
    	    "    </if>" +
    	    "    <if test='info.surplusCount != null'>" +
    	    "      and t.surplus_count  <![CDATA[ > ]]> #{info.surplusCount}  " +
    	    "    </if>" +
    	    "    <if test='info.sortCloum != null'>" +
    	    "     order by ${info.sortCloum}" +
    	    "    </if>" +
    	    
    	    "    <if test='info.startNo != null'>" +
    	    "      limit #{info.startNo},#{info.pageSize}" +
    	    "    </if>" +
    	    "</script>")
    @Results({
        @Result(column="user_id",property="user",one=@One(select="com.hmsg.health.dao.UserDao.select")),
        @Result(column="type",property="typeDo",one=@One(select="com.hmsg.health.dao.TaskTypeDao.findOne"))
    })
    List<TaskDo> select(@Param("info") TaskDo info);
    
    
    @Select("select * from  `s_task` where id !=#{id} and task_status=0")
    @Results({
        @Result(column="user_id",property="user",one=@One(select="com.hmsg.health.dao.UserDao.select")),
        @Result(column="type",property="typeDo",one=@One(select="com.hmsg.health.dao.TaskTypeDao.findOne"))
    })
    List<TaskDo> selectNot(@Param("id") Integer id);
    
    @Delete("delete from s_task where id=#{userId}")
    int delete(Integer userId);
    
  
    @Insert("<script>" +
    "insert into `s_task` " +
    "  <set>" +
    "    <if test='info.type != null'>" +
    "     `type` = #{info.type}, " +
    "    </if>" +
    "    <if test='info.taskCount != null'>" +
    "      `task_count` = #{info.taskCount}, " +
    "    </if>" +
    "    <if test='info.taskGold != null'>" +
    "      `task_gold` = #{info.taskGold}, " +
    "    </if>" +
    "    <if test='info.validity != null'>" +
    "      `validity` = #{info.validity}, " +
    "    </if>" +
    "    <if test='info.taskUrl != null'>" +
    "      `task_url` = #{info.taskUrl}, " +
    "    </if>" +
    "    <if test='info.taskVerify != null'>" +
    "      `task_verify` = #{info.taskVerify}, " +
    "    </if>" +
    "    <if test='info.taskStatus != null'>" +
    "      `task_status` = #{info.taskStatus}, " +
    "    </if>" +
    "    <if test='info.userId != null'>" +
    "      `user_id` = #{info.userId}, " +
    "    </if>" +
    "    <if test='info.taskName != null'>" +
    "      `task_name` = #{info.taskName}, " +
    "    </if>" +
    "    <if test='info.stepOne != null'>" +
    "      `step_one` = #{info.stepOne}, " +
    "    </if>" +
    "    <if test='info.stepTwo != null'>" +
    "      `step_two` = #{info.stepTwo}, " +
    "    </if>" +
    "    <if test='info.slogan != null'>" +
    "      `slogan` = #{info.slogan}, " +
    "    </if>" +
    "    <if test='info.surplusCount != null'>" +
    "      `surplus_count` = #{info.surplusCount}, " +
    "    </if>" +
    "	<if test='info.createTime != null'>" +
    "      `create_time` = #{info.createTime} " +
    "    </if>" +
  
    "  </set>" +
    "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int save(@Param("info") TaskDo info);


    @Update({"<script>",
		"UPDATE  s_task",
		"<trim prefix='set' suffixOverrides=','>",
		
		"<if test='user.surplusCount!=null'>",
		"surplus_count= #{user.surplusCount},",
		"</if>",
		"<if test='user.taskStatus!=null'>",
		"task_status= #{user.taskStatus},",
		"</if>",
		"<if test='user.taskVerify!=null'>",
		"task_verify= #{user.taskVerify},",
		"</if>",
		"<if test='user.isRecommend!=null'>",
		"is_recommend= #{user.isRecommend},",
		"</if>",
	
		"<if test='user.surplusCount!=null'>",
		"surplus_count= #{user.surplusCount}",
		"</if>",
	
		"</trim>",
		"where id = #{user.id}",
		"</script>"})
 	void update(@Param("user") TaskDo user);


	@Select("<script>" +
			" select s.* from s_task  s left join s_user u on s.user_id =u.id" +
			"   where 1=1  " +
			"    <if test='typeId != null '>" +
			"     and s.type=#{typeId} " +
			"    </if>" +
			"    <if test='state != null '>" +
			"     and s.task_status=#{state} " +
			"    </if>" +
			"    <if test='phone != null '>" +
			"     and u.mobile=#{phone} " +
			"    </if>" +
			"    <if test='time != null '>" +
			"   and  DATE_FORMAT(s.create_time, '%Y-%m-%d') = DATE_FORMAT(#{time},'%Y-%m-%d') " +
			"    </if>" +
			"    order by s.id desc" +
			" limit #{firstResult},#{maxResult}"+
			"</script>")
	List<TaskDto>  findAll(@Param("typeId") Integer typeId,
						   @Param("state") Integer state,
						   @Param("phone") String phone,
						   @Param("time") String time,
						   @Param("firstResult") Integer firstResult,
						   @Param("maxResult") Integer maxResult);


	@Select("<script>" +
			" select count(s.id) from s_task  s left join s_user u on s.user_id =u.id" +
			"   where 1=1  " +
			"    <if test='typeId != null '>" +
			"     and s.type=#{typeId} " +
			"    </if>" +
			"    <if test='state != null '>" +
			"     and s.task_status=#{state} " +
			"    </if>" +
			"    <if test='phone != null '>" +
			"     and u.mobile=#{phone} " +
			"    </if>" +
			"    <if test='time != null '>" +
			"   and  DATE_FORMAT(s.create_time, '%Y-%m-%d') = DATE_FORMAT(#{time},'%Y-%m-%d') " +
			"    </if>" +
			"</script>")
	int findCount(@Param("typeId") Integer typeId,
				  @Param("state") Integer state,
				  @Param("phone") String phone,
				  @Param("time") String time);
}